<?php
/**
 * User: Mars 2024/10/7 16:07
 */

namespace hunma\test;

use hunma\basic\ApiResponse;
use hunma\basic\BaseController;
use hunma\utils\IpUtil;
use support\Redis;
use support\think\Db;
use Webman\RedisQueue\Client;

class TestController extends BaseController
{

    /**
     * 成功返回示例
     * @return \support\Response
     * author Fox hunma 2025/2/7 20:19
     */
    public function ApiResponseSuccess(){

        $data = $this->getAllParams();

        return ApiResponse::success($data);
    }

    public function ApiResponseError(){
        return ApiResponse::error('失败');
    }

    public function ValidationRules()
    {
        $rules = [
            ['name', 'require', '名称不能为空'],
            ['email', 'require|email', '邮箱格式不正确'],
            ['password', 'require', '密码不能为空'],
            ['password', 'min:6', '密码长度不能少于6位'],
        ];

        $this->parseValidationRules($rules);

        $data = $this->getAllParams();

        return ApiResponse::success($data);
    }

    public function SysUser(){

        /**
         * 表结构
         * CREATE TABLE `sys_user` (
         * `id` bigint NOT NULL AUTO_INCREMENT COMMENT '用户ID',
         * `dept_id` bigint DEFAULT NULL COMMENT '部门ID',
         * `login_name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '登录账号',
         * `user_name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '' COMMENT '用户昵称',
         * `user_type` varchar(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '00' COMMENT '用户类型（00系统用户 01注册用户）',
         * `email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '' COMMENT '用户邮箱',
         * `phonenumber` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '' COMMENT '手机号码',
         * `sex` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '0' COMMENT '用户性别（0男 1女 2未知）',
         * `avatar` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '' COMMENT '头像路径',
         * `password` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '' COMMENT '密码',
         * `salt` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '' COMMENT '盐加密',
         * `status` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '0' COMMENT '帐号状态（0正常 1停用）',
         * `del_flag` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '0' COMMENT '删除标志（0代表存在 2代表删除）',
         * `login_ip` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '' COMMENT '最后登录IP',
         * `login_date` datetime DEFAULT NULL COMMENT '最后登录时间',
         * `pwd_update_date` datetime DEFAULT NULL COMMENT '密码最后更新时间',
         * `create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '' COMMENT '创建者',
         * `create_time` datetime DEFAULT NULL COMMENT '创建时间',
         * `update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '' COMMENT '更新者',
         * `update_time` datetime DEFAULT NULL COMMENT '更新时间',
         * `remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '备注',
         * `delete_time` datetime DEFAULT NULL COMMENT '删除时间',
         * PRIMARY KEY (`id`) USING BTREE
         * ) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='用户信息表';
 */

        /**
         * 插入语句
         * INSERT INTO `sys_user` (`id`, `dept_id`, `login_name`, `user_name`, `user_type`, `email`, `phonenumber`, `sex`, `avatar`, `password`, `salt`, `status`, `del_flag`, `login_ip`, `login_date`, `pwd_update_date`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`) VALUES (1, 103, 'admin', '若依', '00', 'ry@163.com', '15888888888', '1', '', '2e030f4ec30f015da3ffa0cdda972119', '111111', '0', '0', '127.0.0.1', NULL, NULL, 'admin', '2025-02-04 13:55:42', '', NULL, '管理员');
         * INSERT INTO `sys_user` (`id`, `dept_id`, `login_name`, `user_name`, `user_type`, `email`, `phonenumber`, `sex`, `avatar`, `password`, `salt`, `status`, `del_flag`, `login_ip`, `login_date`, `pwd_update_date`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`) VALUES (2, 105, 'ry', '若依', '00', 'ry@qq.com', '15666666666', '1', '', '8e6d98b90472783cc73c17047ddccf36', '222222', '0', '0', '127.0.0.1', NULL, NULL, 'admin', '2025-02-04 13:55:42', '', NULL, '测试员');
 */

        $data['dbFind'] = Db::table('sys_user')->find(1);

        $data['getList'] = SysUserModelTest::getList();
        $data['allList'] = SysUserModelTest::allList();
        // $data['getOne'] = SysUserModel::getOne(['user_id'=>1]);
        // 其他的具体说明在：hunma/basic/BaseModel.php:3

        return ApiResponse::success($data);
    }

    public function Redis()
    {
        // 详细：https://www.workerman.net/doc/webman/db/redis.html
        $key = 'test_key';
        Redis::set($key, rand());
        return ApiResponse::success(Redis::get($key));
    }

    public function RedisConsumer()
    {
        $msg = '测试传入数据';
        Client::send('test_queue', $msg);
        return ApiResponse::success($msg);

    }

    public function IpUtil()
    {
        $data['ip'] = IpUtil::getClientIp(); // 当前ip
        $data['user_agent'] = IpUtil::getUserAgent(); // 用户头
        $data['user_agent_info'] = IpUtil::parseUserAgent($data['user_agent']); // 头的详细信息
        $data['getLocation'] = IpUtil::getLocation($data['ip']); // 根据ip获取详细地址

        return ApiResponse::success($data);
    }

    public function Uuid()
    {
        $data = bin2hex(random_bytes(16));
        return ApiResponse::success($data);

    }

    public function menu()
    {
        print_r(333333333);
        $json = '{
  "code": 0,
  "msg": "success",
  "data": {
    "menus": [
      {
        "id": 1,
        "parent_id": 0,
        "title": "仪表盘",
        "icon": "home",
        "path": "/dashboard",
        "component": "dashboard/index",
        "sort": 1,
        "hidden": false,
        "permission": "dashboard:view",
        "type": "menu"
      },
      {
        "id": 2,
        "parent_id": 0,
        "title": "系统管理",
        "icon": "setting",
        "path": "system",
        "component": null,
        "sort": 2,
        "hidden": false,
        "permission": "system:view",
        "type": "directory",
        "children": [
          {
            "id": 3,
            "parent_id": 2,
            "title": "用户管理",
            "icon": "user",
            "path": "/system/user",
            "component": "system/user/index",
            "sort": 1,
            "hidden": false,
            "permission": "user:view",
            "type": "menu"
          },
          {
            "id": 4,
            "parent_id": 2,
            "title": "角色管理",
            "icon": "usergroup",
            "path": "/system/role",
            "component": "system/role/index",
            "sort": 2,
            "hidden": false,
            "permission": "role:view",
            "type": "menu"
          },
          {
            "id": 5,
            "parent_id": 2,
            "title": "权限设置",
            "icon": "key",
            "path": "/system/permission",
            "component": "system/permission/index",
            "sort": 3,
            "hidden": false,
            "permission": "permission:view",
            "type": "menu"
          }
        ]
      },
      {
        "id": 6,
        "parent_id": 0,
        "title": "配置管理",
        "icon": "setting",
        "path": "config",
        "component": null,
        "sort": 3,
        "hidden": false,
        "permission": "config:view",
        "type": "directory",
        "children": [
          {
            "id": 7,
            "parent_id": 6,
            "title": "菜单管理",
            "icon": "list",
            "path": "/config/menu",
            "component": "config/menu/index",
            "sort": 1,
            "hidden": false,
            "permission": "menu:view",
            "type": "menu"
          },
          {
            "id": 8,
            "parent_id": 6,
            "title": "权限配置",
            "icon": "key",
            "path": "/config/permission",
            "component": "config/permission/index",
            "sort": 2,
            "hidden": false,
            "permission": "permission:config",
            "type": "menu"
          },
          {
            "id": 9,
            "parent_id": 6,
            "title": "系统设置",
            "icon": "setting",
            "path": "/config/settings",
            "component": "config/settings/index",
            "sort": 3,
            "hidden": false,
            "permission": "settings:view",
            "type": "menu"
          }
        ]
      },
      {
        "id": 10,
        "parent_id": 0,
        "title": "日志管理",
        "icon": "history",
        "path": "log",
        "component": null,
        "sort": 4,
        "hidden": false,
        "permission": "log:view",
        "type": "directory",
        "children": [
          {
            "id": 11,
            "parent_id": 10,
            "title": "操作日志",
            "icon": "history",
            "path": "/log/operation",
            "component": "log/operation/index",
            "sort": 1,
            "hidden": false,
            "permission": "log:operation:view",
            "type": "menu"
          },
          {
            "id": 12,
            "parent_id": 10,
            "title": "登录日志",
            "icon": "calendar",
            "path": "/log/login",
            "component": "log/login/index",
            "sort": 2,
            "hidden": false,
            "permission": "log:login:view",
            "type": "menu"
          },
          {
            "id": 13,
            "parent_id": 10,
            "title": "错误日志",
            "icon": "alert",
            "path": "/log/error",
            "component": "log/error/index",
            "sort": 3,
            "hidden": false,
            "permission": "log:error:view",
            "type": "menu"
          }
        ]
      },
      {
        "id": 14,
        "parent_id": 0,
        "title": "测试管理",
        "icon": "history",
        "path": "test",
        "component": null,
        "sort": 5,
        "hidden": false,
        "permission": "test:view",
        "type": "directory",
        "children": [
          {
            "id": 15,
            "parent_id": 14,
            "title": "访问统计",
            "icon": "history",
            "path": "/test/test",
            "component": "test/statistics/index",
            "sort": 1,
            "hidden": false,
            "permission": "test:test:view",
            "type": "menu"
          },
          {
            "id": 16,
            "parent_id": 14,
            "title": "示例演示",
            "icon": "star",
            "path": "/test/demo",
            "component": "test/demo/index",
            "sort": 2,
            "hidden": false,
            "permission": "test:demo:view",
            "type": "menu"
          },
          {
            "id": 17,
            "parent_id": 14,
            "title": "多级菜单",
            "icon": "apps",
            "path": "sub",
            "component": null,
            "sort": 3,
            "hidden": false,
            "permission": "test:sub:view",
            "type": "directory",
            "children": [
              {
                "id": 18,
                "parent_id": 17,
                "title": "子页面1",
                "icon": "file",
                "path": "/test/sub/page1",
                "component": "test/sub/page1/index",
                "sort": 1,
                "hidden": false,
                "permission": "test:sub:page1:view",
                "type": "menu"
              },
              {
                "id": 19,
                "parent_id": 17,
                "title": "子页面2",
                "icon": "file",
                "path": "/test/sub/page2",
                "component": "test/sub/page2/index",
                "sort": 2,
                "hidden": false,
                "permission": "test:sub:page2:view",
                "type": "menu"
              },
              {
                "id": 20,
                "parent_id": 17,
                "title": "三级菜单",
                "icon": "code",
                "path": "level3",
                "component": null,
                "sort": 3,
                "hidden": false,
                "permission": "test:sub:level3:view",
                "type": "directory",
                "children": [
                  {
                    "id": 21,
                    "parent_id": 20,
                    "title": "三级页面",
                    "icon": "file",
                    "path": "/test/sub/level3/page",
                    "component": "test/sub/level3/page/index",
                    "sort": 1,
                    "hidden": false,
                    "permission": "test:sub:level3:page:view",
                    "type": "menu"
                  },
                  {
                    "id": 22,
                    "parent_id": 20,
                    "title": "四级菜单",
                    "icon": "code",
                    "path": "deep",
                    "component": null,
                    "sort": 2,
                    "hidden": false,
                    "permission": "test:sub:level3:deep:view",
                    "type": "directory",
                    "children": [
                      {
                        "id": 23,
                        "parent_id": 22,
                        "title": "四级页面",
                        "icon": "file",
                        "path": "/test/sub/level3/deep/page",
                        "component": "test/sub/level3/deep/page/index",
                        "sort": 1,
                        "hidden": false,
                        "permission": "test:sub:level3:deep:page:view",
                        "type": "menu"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
}';
        return ApiResponse::success(json_decode($json));
    }
}